<script>
	import {
		mapState,
		mapMutations
	} from 'vuex';

	export default {
		methods: {
			...mapMutations(['login']),
			/*
				缓存登录状态
			*/
			cacheLoginStatus() {
				uni.getStorage({
					key: 'userInfo',
					success: (res) => {
						this.login(res.data)
					}
				})
			},
			/*
				获取个人信息
			 */
			onGetUserInfo() {
				uni.getUserInfo({
					success: (res) => {
						console.log(res)
						// const {avatarUrl,nickName} = res.userInfo;
						// this.login({
						// 	avatarUrl,
						// 	nickName
						// })
						this.exchangeOpenId(res.userInfo)
					}
				})
			},
			/*
			   换取openid
			*/
			exchangeOpenId(data) {
				uni.login({
					provider: 'weixin',
					success: (res) => {
						wx.request({
							url: 'https://wechat.lccmart.com/ygy/api/wechatapi/get_openid',
							data: {
								code: res.code,
							},
							success: (info) => {
								const {
									avatarUrl,
									nickName
								} = data;
								const openid = info.data.openid;
								this.login({
									avatarUrl,
									nickName,
									openid
								})
							}
						})
					}
				});
			}
		},
		computed: {
			...mapState(['hasLogin'])
		},
		onLaunch: function() {
			// const flag = uni.getStorageSync('hasLogin');
			
			// if (flag) {
			// 	uni.switchTab({
			// 		url: 'pages/index/index'
			// 	})
			// }
		},
		onShow: function() {},
		onHide: function() {},
	}
</script>

<style>
	@import "colorui/main.css";
	@import "colorui/icon.css";

	.mar_l {
		margin-left: 20rpx;
	}

	.font_24 {
		font-size: 24rpx;
	}

	.font_999 {
		color: #999;
	}

	.padding25rpx {
		padding: 0 25rpx;
	}

	.relative {
		position: relative;
	}
	.cu-form-group .title {
		min-width: calc(5em + 15px);
	}
</style>
